// pages/orderForm/orderForm.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    orders: [],
    storeImgs: [],
    states: ['待确认', '已完成'],
    orderState: '',
    orderId: '',
    user:''
  },
  // 进行评价
  comment(e) {
    console.log(e);
    wx.navigateTo({
      url: "/pages/addComment/addComment?index=" + e.currentTarget.id+'&&orderId='+e.currentTarget.dataset.item._id
    })
  },
  toStore(e){
    let store = e.currentTarget.dataset.item.store
    console.log(store);
    wx.navigateTo({
      url: '/pages/stores/stores?storeName='+store
    })
  },
  // 修改订单状态
  confirmOrderStateHandler(e) {
    wx.showModal({
      cancelText: '取消',
      confirmColor: 'red',
      confirmText: '确认',
      content:'你确定收到货品了吗？',
      title: '提示',
      success: (result) => {
        if (result.confirm) {
          this.setData({
            state: this.data.states[1],
            orderState: this.data.states[1],
            orderId: e.currentTarget.dataset.item._id
          })
          setTimeout((res) => {
            this.yunUpdateOrderState(this.data.orderId)
            this.onShow()
          }, 500)
        }
      }
    })
  },
  // 点击删除订单
  deleteOrder(e){
    let orderId = e.currentTarget.dataset.item._id
    wx.showModal({
      cancelText: '取消',
      confirmColor: 'red',
      confirmText: '确认',
      content:'你确定要删除该订单吗？',
      title: '提示',
      success: (res) => {
        if (res.confirm) {
          this.yunDeleteOrder(orderId)
          if (!this.data.orders[1]) {
            this.setData({orders:[]})
          }
        }
      }
    })
  },
  // 获取全部订单
  yunGetOrders(user) {
    let that = this
    let db = wx.cloud.database()
    db.collection('CR_orders')
      .where({
        user:user
      })
      .get({
        success: (res) => {
          that.setData({
            orders: res.data,
            orderState: res.data[0].state
          })
          if (this.data.orderState) {
            this.setData({
              hint: false
            })
          }
          this.yunGetStoreImgPath(that.data.orders[0].store)
          console.log(res);
        }
      })
  },
  // 云数据删除订单
  yunDeleteOrder(orderId){
    let user=this.data.user
    let db = wx.cloud.database()
    let that = this
    db.collection('CR_orders')
      .doc(orderId)
      .remove({
        success:(res)=>{
          wx.showToast({
            title: '删除成功',
            duration: 800,
            icon: 'success',
            success: (res) => {
              that.yunGetOrders(user)
              that.onLoad()
            }
          })
        }
      })
  },
  // 与数据修改订单
  yunUpdateOrderState(orderId) {
    let db = wx.cloud.database()
    let that = this
    db.collection('CR_orders')
      .doc(orderId)
      .update({
        data: {
          state: this.data.orderState
        },
        success: (res) => {
          that.onLoad()
        }
      })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({user:wx.getStorageSync('Tel')})
    this.yunGetOrders(this.data.user)
  },
  
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
    
  },
  
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.setData({user:wx.getStorageSync('Tel')})
    this.yunGetOrders(this.data.user)
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    wx.reLaunch({
      url: '/pages/orderForm/orderForm'
    })
    wx.showToast({
      title: '刷新成功!',
      duration: 800,
      icon: 'none'
    })
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    wx.showToast({
      title: '已经到底啦>_<',
      duration: 800,
      icon: 'none'
    })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})